package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.GoodImage;
import com.wuming.house.model.HouseLayout;
import com.wuming.house.service.GoodImageService;
import com.wuming.house.service.HouseLayoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web/houselayout")
public class HouseLayoutController extends BaseController {

    @Autowired
    private HouseLayoutService houseLayoutService;

    @Autowired
    private GoodImageService goodImageService;

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam Long houseId, @RequestParam(required = false) String layoutName, @RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<HouseLayout> houseLayouts = houseLayoutService.getHouseLayoutByPage(houseId, layoutName, pageIndex, pageSize);
            int total = houseLayoutService.getTotal(houseId, layoutName);
            return CommonResult.createSuccessResult(houseLayouts, total, "查询所有楼盘户型成功");
        } catch (Exception e) {
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询所有楼盘户型失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Long id) {
        logger.info("准备删除id:{}的户型", id);
        try {
            houseLayoutService.removeHouseLayoutById(id);
            return new JsonResult(null, "删除成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return new JsonResult(null, "删除失败", false);
        }
    }

    @RequestMapping("/toAddLayout")
    public String toAddLayout(@RequestParam Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "/house/editHouseLayout";
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file) {
        try {
            String path = this.uploadImage(file, "houselayout", 1);
            GoodImage goodImage = new GoodImage();
            goodImage.setImagePath(path);
            goodImage.setCreateDatetime(new Date());
            goodImageService.saveImage(goodImage);
            return new JsonResult(goodImage.getId(), "上传成功", true);
        } catch (IOException ie) {
            logger.error("上传户型图片失败,异常:{}", ie.getMessage());
            return new JsonResult("", "上传失败", false);
        } catch (Exception e) {
            logger.error("上传户型图片失败,异常:{}", e.getMessage());
            return new JsonResult("", "上传失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(HouseLayout layout) {
        try {
            if (null == layout.getId()) {
                layout.setCreateDatetime(new Date());
                GoodImage gi = goodImageService.getImageById(layout.getPicId().intValue());
                layout.setPicUrl(gi.getImagePath());
                houseLayoutService.saveHouseLayout(layout);
            } else {
                houseLayoutService.modifyHouseLayout(layout);
            }
            return new JsonResult(layout, "保存成功", true);
        } catch (Exception e) {
            logger.error("保存失败,异常:{}", e.getMessage());
            return new JsonResult(null, "保存失败", false);
        }
    }

}
